package com.example.suspensionlist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.suspensionlist.adapter.MultipleItemQuickAdapter;
import com.example.suspensionlist.data.DataServer;
import com.example.suspensionlist.entity.MultipleItem;

import org.w3c.dom.Text;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private View mSuspensionBar;
    private int mSuspensionHeight;
    private int mCurrentPosition = 0;
    private List<MultipleItem> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = findViewById(R.id.recycler_view);
        mSuspensionBar = findViewById(R.id.suspension_bar);

        mData = DataServer.getMultipleItemData();
        final MultipleItemQuickAdapter multipleItemAdapter = new MultipleItemQuickAdapter(this, mData);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(multipleItemAdapter);

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                mSuspensionHeight = mSuspensionBar.getHeight();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (multipleItemAdapter.getItemViewType(mCurrentPosition + 1) == MultipleItem.LARGE) {
                    View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
                    if (view != null) {
//                        if (view.getTop() <= mSuspensionHeight) {
//                            mSuspensionBar.setY(-(mSuspensionHeight - view.getTop()));
//                        } else {
//                            mSuspensionBar.setY(0);
//                        }
                    }
                }

                if (mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()) {
                    mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    mSuspensionBar.setY(0);

                    updateSuspensionBar();
                }
            }
        });

        updateSuspensionBar();
    }

    private void updateSuspensionBar() {
        TextView tvContent = mSuspensionBar.findViewById(R.id.tv);
        View tvDetails = mSuspensionBar.findViewById(R.id.tv_details);

//        MultipleItem multipleItem = mData.get(mCurrentPosition);
//
//        if (multipleItem.getItemType() == MultipleItem.LARGE) {
//            tvContent.setText(multipleItem.getContent());
//            tvDetails.setVisibility(multipleItem.isVisibleState() ? View.VISIBLE : View.GONE);
//        }else {
//
//        }

        MultipleItem multipleItem = mData.get(getMyLargePosition(mCurrentPosition));

        if (multipleItem.getItemType() == MultipleItem.LARGE) {
            tvContent.setText(multipleItem.getContent());
            tvDetails.setVisibility(multipleItem.isVisibleState() ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 得到此时item所在的大item位置
     *
     * @param position
     * @return
     */
    private int getMyLargePosition(int position) {
        MultipleItem multipleItem = mData.get(position);

        if (multipleItem.getItemType() == MultipleItem.LARGE) {
            return position;
        }

        return getMyLargePosition(--position);
    }
}
